import * as Sentry from '@sentry/vue'

/**
 * 手动捕获异常
 * @param error 错误对象
 * @param context 额外上下文信息
 */
export function captureError(error: Error, context?: Record<string, any>) {
  if (context) {
    Sentry.withScope((scope) => {
      Object.keys(context).forEach(key => {
        scope.setContext(key, context[key])
      })
      Sentry.captureException(error)
    })
  } else {
    Sentry.captureException(error)
  }
}

/**
 * 记录消息
 * @param message 消息内容
 * @param level 日志级别
 */
export function captureMessage(message: string, level: 'info' | 'warning' | 'error' = 'info') {
  Sentry.captureMessage(message, level)
}

/**
 * 设置用户信息
 * @param user 用户信息
 */
export function setUser(user: { id?: string; email?: string; username?: string }) {
  Sentry.setUser(user)
}

/**
 * 添加标签
 * @param key 标签键
 * @param value 标签值
 */
export function setTag(key: string, value: string) {
  Sentry.setTag(key, value)
}